<?php
/**
 * QQAS - share your knowledge to the world!
 * 
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */
 
// ------------------------------------------------------------------------

/**
 * markup 标签解析
 *
 * @category  Util
 */
class MarkupParser
{

    //解析bbcode
	public static function bbcode2html($text)
	{
		$text = htmlspecialchars($text, ENT_NOQUOTES);
		if (!function_exists('escape'))
		{
			function escape($s)
			{
				global $text;
				$text = strip_tags($text);
				$code = $s[1];
				$code = str_replace("[", "&#91;", $code);
				$code = str_replace("]", "&#93;", $code);
				return '<pre><code>'.$code.'</code></pre>';
			}
		}
		$text = preg_replace_callback('/\[code\](.*?)\[\/code\]/ms', "escape", $text);
		$in = array(
			'/\[b\](.*?)\[\/b\]/ms',
			'/\[i\](.*?)\[\/i\]/ms',
			'/\[u\](.*?)\[\/u\]/ms',
			'/\[img\](.*?)\[\/img\]/ms',
			'/\[email\](.*?)\[\/email\]/ms',
			'/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
			'/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
			'/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
			'/\[quote](.*?)\[\/quote\]/ms',
			'/\[list\=(.*?)\](.*?)\[\/list\]/ms',
			'/\[list\](.*?)\[\/list\]/ms',
			'/\[\*\]\s?(.*?)\n/ms',
		);
		$out = array(
			'<strong>\1</strong>',
			'<em>\1</em>',
			'<u>\1</u>',
			'<img src="\1" alt="\1" />',
			'<a href="mailto:\1">\1</a>',
			'<a href="\1">\2</a>',
			'<span style="font-size:\1%">\2</span>',
			'<span style="color:\1">\2</span>',
			'<blockquote>\1</blockquote>',
			'<ol class="ubb" start="\1">\2</ol>',
			'<ul class="ubb">\1</ul>',
			'<li>\1</li>',
		);
		$text = preg_replace($in, $out, $text);
		$text = str_replace("\r", "", $text);
		$text = nl2br($text);
		if (!function_exists('removeBr'))
		{
			function removeBr($s)
			{
				return str_replace("<br />", "", $s[0]);
			}
		}
		$text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "removeBr", $text);
		$text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $text);
		$text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "removeBr", $text);
		$text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $text);
		return $text;
	}
	
	
	// 解析wiki
	public static function wiki2html($text)
	{
		$text = htmlspecialchars($text, ENT_NOQUOTES);
		$in = array(
			'/={7}(.*?)={7}\n?/', //h5
			'/={6}(.*?)={6}\n?/', //h5
			'/={5}(.*?)={5}\n?/', //h4
			'/={4}(.*?)={4}\n?/', //h3
			'/={3}(.*?)={3}\n?/', //h2
			'/={2}(.*?)={2}\n?/', //h1
			"/'''(.*?)'''/", //bold
			"/''(.*?)''/", //italic
			'/&lt;s&gt;(.*?)&lt;\/s&gt;/', //stroke
			'/\[\[Image:(.*?)\|(.*?)\]\]/', //img
			'/\[(.*?)\s+(.*?)\]/', // link
			'/\*\s+(.*?)\n/', // ul->li
			'/#\s+(.*?)\n/', // ol->li
			'/&lt;source lang="(.*?)"&gt;(.*?)&lt;\/source&gt;/', //code
			'/&gt;\s+(.*?)\n/', //quote
		);
		$out = array(
			'<h6>\1</h6>',
			'<h5>\1</h5>',
			'<h4>\1</h4>',
			'<h3>\1</h3>',
			'<h2>\1</h2>',
			'<h1>\1</h1>',
			'<b>\1</b>',
			'<i>\1</i>',
			'<s>\1</s>',
			'<img src="\1" alt="\2" title="\2" />',
			'<a href="\1" title="\2">\2</a>',
			'<ul><li>\1</li></ul>',
			'<ol><li>\1</li></ol>',
			'<pre lang="\1">\2</pre>',
			'<blockquote>\1</blockquote>',
		);
		$text = preg_replace($in, $out, $text);
		$text = str_replace('</ul><ul>', '', $text);
		$text = str_replace('</ol><ol>', '', $text);
		$text = nl2br($text);
		return $text;
	}
	
	
	// 解析markdown
	public static function markdown2html($text)
	{
		$text = htmlspecialchars($text, ENT_NOQUOTES);
		$in = array(
			'/#{6}(.*?)\n/', //h6
			'/#{5}(.*?)\n/', //h5
			'/#{4}(.*?)\n/', //h4
			'/#{3}(.*?)\n/', //h3
			'/(.*?)\n-+\n/', //h2
			'/(.*?)\n=+\n/', //h1
			"/\*{2}(.*?)\*{2}/", //bold
			"/_(.*?)_/", //italic
			'/!\[(.*?)\]\((.*?)\s+"(.*?)"/', // img
			'/\[(.*?)\]\((.*?)\s+"(.*?)"/', //link
			'/-\s+(.*?)\n/', // ul->li
			'/\d\.\s+(.*?)\n/', // ol->li
			'/\t(.*?)\n/', //code
			'/&gt;\s+(.*?)\n/s', //quote
		);
		$out = array(
			'<h6>\1</h6>',
			'<h5>\1</h5>',
			'<h4>\1</h4>',
			'<h3>\1</h3>',
			'<h2>\1</h2>',
			'<h1>\1</h1>',
			'<b>\1</b>',
			'<i>\1</i>',
			'<img src="\1" alt="\2" title="\2" />',
			'<a href="\1" title="\2">\2</a>',
			'<ul><li>\1</li></ul>',
			'<ol><li>\1</li></ol>',
			'<pre lang="\1">\2</pre>',
			'<blockquote>\1</blockquote>',
		);
		$text = preg_replace($in, $out, $text);
		$text = str_replace('</ul><ul>', '', $text);
		$text = str_replace('</ol><ol>', '', $text);
		$text = nl2br($text);
		return $text;
	}

}
// End  Class

/* End of file Parser.class.php */
/* Location: .App/Lib/Util/Parser.class.php */